/**
 * Created by wzh on 2016/5/27.
 */
'use strict';
swan_pc
    .controller("MainPageController",function($rootScope,$scope,$q,Clip,newMessageManage,$state,localStorageService,$http,$timeout){
        // console.log("MainPage Run");

        //localStorageService 值查看
        //console.log(localStorageService);
        //登陆后弹框
        $scope.AccountMessageAlert=0;
        $scope.ClipsBookByOthersNoReply=0;
        $scope.MessageNoReply=0;
        $scope.AlertString="";
        $scope.AlertShowFlag=0;
        var def=$q.defer();
        var pro=def.promise;
        Clip.query({},function(result){
            $scope.clips=result;
            angular.forEach($scope.clips,function(data,index,array){
                if(data.bookerId!=null && data.status=="DRAFT"){
                    $scope.ClipsBookByOthersNoReply=$scope.ClipsBookByOthersNoReply+1;
                }
            });
            def.resolve();
        });
        pro.then(function(){
            var def1=$q.defer();
            var pro1=def1.promise;
            newMessageManage.query(function(result){
                $scope.message=result;
                angular.forEach($scope.message,function(data,index,array){
                    if(!data.isRead){
                        $scope.MessageNoReply=$scope.MessageNoReply+1;
                    }
                });
                def1.resolve();
            });
            pro1.then(function(){
                if($scope.ClipsBookByOthersNoReply>0){
                    if($scope.MessageNoReply>0){
                        $scope.AlertString="您有"+$scope.ClipsBookByOthersNoReply+"条好友为您book的日程、"+$scope.MessageNoReply+"条好友发送给您的短信未处理，请及时处理！";
                    }else{
                        $scope.AlertString="您有"+$scope.ClipsBookByOthersNoReply+"条好友为您book的日程未处理，请及时处理！"
                    }
                }else{
                    if($scope.MessageNoReply>0){
                        $scope.AlertString="您有"+$scope.MessageNoReply+"条好友发送给您的短信未处理，请及时处理！"
                    }else{

                    }
                }
                if($scope.AlertString!=""){
                    $scope.AlertShowFlag=1;
                }
            })
        });
        $scope.closeThisAlert=function(){
            $scope.AlertShowFlag=0;
        };


        //chat
        $scope.chatList=[];

        $scope.input="";
        $scope.submit=function(){
            $scope.chat={
                "time":new Date(),
                "type":0,
                "msg":$scope.input,
                "img":"assets/img/chat/headshot2.jpg",
                "name":"聪明的你"
            };
            $scope.chatList.push($scope.chat);
            $scope.tuLingJson={
                "key":"07d7f2aa8c0c4e66b4ee18c33ef4ae1b",
                "info":$scope.input,
                "loc":"北京市门头沟区",
                "userid":$rootScope.presentAccountInfo.id
            };

            // $http({
            //     method:"POST",
            //     url:"http://www.tuling123.com/openapi/api",
            //     dateType:"jsonp",
            //     data:$scope.toLingJson,
            //     headers:{"Access-Control-Allow-Origin":'www.tuling123.com'}
            // }).success(function(req){
            //     console.log(req);
            // })
            $http({
                method:"GET",
                url:"http://www.tuling123.com/openapi/api?key=07d7f2aa8c0c4e66b4ee18c33ef4ae1b&info="+$scope.input+"&userid="+$rootScope.presentAccountInfo.id
            }).success(function(req){
                $scope.input="";
                console.log(req);
                $scope.chat={
                    "time":new Date(),
                    "type":1,
                    "msg":req.text,
                    "link":req.url,
                    "img":"assets/img/chat/headshot1.jpg",
                    "name":"李大哥的机器人",
                    "list":req.list
                };
                $scope.chatList.push($scope.chat);
                $timeout(function(){
                    $scope.refresh();
                },100);
            })
        };
        $scope.press=function(str){
            if(event.keyCode==13){
                $scope.submit();
            }
        };
        $scope.openLink=function(str){
            javascript:window.open(str);
        };
        $scope.refresh=function(){
            var div = document.getElementById("chat-window");
            div.scrollTop = div.scrollHeight;
        }

    });


